﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Poco;

namespace Metier
{
    public class GestionRole : RoleProvider
    {
        public override bool IsUserInRole(string username, string roleName)
        {
            Utilisateur user = null;

            using (Context db = new Context())
            {
                user = (from u in db.Utilisateurs
                        where u.Email == username
                        select u).FirstOrDefault();
            }

            if (user.GetType().ToString().Contains(roleName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string email)
        {
            Utilisateur user = null;

            using (Context db = new Context())
            {
                user = (from u in db.Utilisateurs
                        where u.Email == email
                        select u).FirstOrDefault();
            }

            string[] roles = new string[1];
            roles[0] = user.GetType().Name.ToString().Split('_')[0];
            return roles;
        }

        public override string[] GetUsersInRole(string roleName)
        {

            String[] users = new string[2];
  
            users[0] = "Georges !!!";
            users[1] = "Niiooooon";

            Console.WriteLine("The cake is a lie");
            return users;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}
